package org.javaboy.vhr.service;
/**
 * ClassName: ApplyInsService <br/>
 * Description: <br/>
 * date: 2020/3/2 20:20<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

import org.apache.ibatis.annotations.Select;
import org.javaboy.vhr.mapper.BorrowCarMapper;
import org.javaboy.vhr.model.BorrowBean;
import org.javaboy.vhr.model.BorrowCar;
import org.javaboy.vhr.model.Hr;
import org.javaboy.vhr.model.RespPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @program: vhr
 * @description:
 * @author: hesion
 * @create: 2020-03-02 20:20
 **/
@Service
public class BorrowCarService {
    @Autowired
    BorrowCarMapper borrowCarMapper;

    public List<Hr> getList() {
        return borrowCarMapper.getList();
    }

    public int apply(BorrowCar borrowCar) {
        borrowCar.setId(UUID.randomUUID().toString());
        borrowCar.setCreateTime(new Date());
        borrowCar.setCreateBy(SecurityContextHolder.getContext().getAuthentication().getName());
        borrowCar.setDel_flag("0");
        int apply = borrowCarMapper.apply(borrowCar);
        if (apply >= 0) {
            return borrowCarMapper.updateBorrow(borrowCar);
        } else {
            return 0;
        }
    }

    public RespPageBean getBorrowByPage(Integer page, Integer size, BorrowBean borrowBean, Date[] beginDateScope) {
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        List<BorrowBean> data = borrowCarMapper.getBorrowByPage(page, size, borrowBean, beginDateScope);
        Long total = borrowCarMapper.getTotal(borrowBean, beginDateScope);
        RespPageBean bean = new RespPageBean();
        bean.setData(data);
        bean.setTotal(total);
        return bean;
    }

    public int backCar(BorrowBean borrowBean) {
        int update= borrowCarMapper.updateData(borrowBean);
        int i = borrowCarMapper.backCar(borrowBean.getId());
        if (i > 0 && update > 0 ) {
            return borrowCarMapper.updateBack(borrowBean.getId());
        } else {
            return 0;
        }
    }

    public int changeMileAndSpend(BorrowCar borrowCar) {
        borrowCar.setUpdateTime(new Date());
        borrowCar.setUpdateBy(SecurityContextHolder.getContext().getAuthentication().getName());
        return borrowCarMapper.changeMileAndSpend(borrowCar);
    }
}
